package router

import (
	"RocFamilySystem/internal/controller"
	"RocFamilySystem/internal/repository"
	"RocFamilySystem/internal/service"
	"RocFamilySystem/router/middleware"
	"RocFamilySystem/tools/container"
	"github.com/gin-gonic/gin"
)

// InitRouter 初始化路由
func InitRouter() {
	engine, _ := container.GetBeanFromContainer("gin.Engine").(*gin.Engine)
	useMiddleware(engine)

	//初始化持久层
	repository.InitRepository()
	//初始化业务层
	service.InitService()
	//初始化控制层
	controller.InitController()

	//初始化公共模块
	(&controller.CommonsController{
		SysUserService: container.GetBeanFromContainer("service.SysUserService").(*service.SysUserService),
	}).RegisterRouter(engine)
}

// 使用中间件
func useMiddleware(engine *gin.Engine) {
	//处理错误信息
	engine.Use(middleware.HandleErr())
	//验证用户是否登录
	engine.Use(middleware.Authority())
	//检查用户权限
	engine.Use(middleware.HandlePermission())
}
